﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Web.Core;
using Web.Core.Objects;

using System.Collections.Generic;

public partial class include_categorys : System.Web.UI.UserControl
{
    public string categoryName;
    ArrayList list;
    int total = 5;
    int page = 1;
    int catID;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (Request.Params["page"] != null)
                page = int.Parse(Request.Params["page"].ToString());
            catID = int.Parse(Request.Params["catid"].ToString());

            if (Request.Params["sort"] != null && Request.Params["sortType"] != null)
                GetDataByParamSort(SetValueParamSort(Request.Params["sort"].ToString()), catID, SetValueParamSortType(Request.Params["sortType"].ToString()));
            else
                GetDataByParamSort(GlobalStorage.ProductsOrderBy.LatestProducts, catID, GlobalStorage.Order.Descend);

            this.categoryName = Categories.GetCategoryByID(catID, (int)Session["Language"]).Name;
            this.Pager1.objectsPerPage = total;
            this.Pager1.displayAll = true;
            this.Pager1.objectsName = "Sản Phẩm";
            this.Pager1.totalObjects = Products.CountAllProductsByCategoryIDVisible((int)Session["Language"], catID);
        }
    }

    //void SetValueDropDownList(DropDownList ddl)
    //{
    //    switch (ddl.ID)
    //    {
    //        case "DropDownList1":
    //            if(!string.IsNullOrEmpty(Request.Params["sort"].ToString()))
    //            {
    //                switch(Request.Params["sort"].ToString())
    //                {
    //                    case "product_default":
    //                        this.DropDownList1.SelectedIndex = 0;
    //                        break;
    //                    case "product_name":
    //                        this.DropDownList1.SelectedIndex = 1;
    //                        break;
    //                    case "product_price":
    //                        this.DropDownList1.SelectedIndex = 2;
    //                        break;
                        
    //                    default:
    //                        this.DropDownList1.SelectedIndex = 3;
    //                        break;
    //                }
    //            }
    //            break;
    //        case "DropDownList3":
    //            if(!string.IsNullOrEmpty(Request.Params["sortType"].ToString()))
    //            {
    //                switch(Request.Params["sortType"].ToString())
    //                {
    //                    case "asc":
    //                        this.DropDownList3.SelectedIndex = 1;
    //                        break;
    //                    case "desc":
    //                        this.DropDownList3.SelectedIndex = 2;
    //                        break;
    //                    default:
    //                        this.DropDownList3.SelectedIndex = 1;
    //                        break;
    //                }
    //            }
    //            break;
    //    }
    //}


    GlobalStorage.Order SetValueParamSortType(string param)
    {
        try
        {
            if (!string.IsNullOrEmpty(param))
            {
                switch (param.ToLower())
                {
                    case "asc":
                        return GlobalStorage.Order.Ascend;
                    case "desc":
                        return GlobalStorage.Order.Descend;
                }
            }
            return GlobalStorage.Order.Ascend;
        }
        catch
        {
            return GlobalStorage.Order.Ascend;
        }

    }

    GlobalStorage.ProductsOrderBy SetValueParamSort(string param)
    {
        try
        {
            if (!string.IsNullOrEmpty(param))
            {
                switch (param)
                {
                    case "product_name":
                        return GlobalStorage.ProductsOrderBy.ProductName;
                    case "product_price":
                        return GlobalStorage.ProductsOrderBy.Price;
                    case "product_cdate":
                        return GlobalStorage.ProductsOrderBy.LatestProducts;
                    default:
                        return GlobalStorage.ProductsOrderBy.Default;
                }
            }
            return GlobalStorage.ProductsOrderBy.Default;
        }
        catch
        {
            return GlobalStorage.ProductsOrderBy.Default;
        }
    }

    void GetDataByParamSort(GlobalStorage.ProductsOrderBy productSort, int catID, GlobalStorage.Order order)
    {
        ShowProductsOrder(getAllProductByCategory(page, catID, total, productSort, order));
    }

    private void ShowProductsOrder(string source)
    {
        int catID = int.Parse(Request.Params["catid"].ToString());
        this.products.Text = source;
    }

    private string getAllProductByCategory(int page, int catID, int total, GlobalStorage.ProductsOrderBy orderBy, GlobalStorage.Order order)
    {

        if (Request.Params["page"] != null)
            page = int.Parse(Request.Params["page"].ToString());

        list = new ArrayList();
        list = Products.GetAllProductsByCategoryIDVisible((int)Session["Language"], catID, page, total, orderBy, order);

        string str = ShowProducts(list);
        return str;
    }

    private string ShowProducts(ArrayList _listProduct)
    {
        string str = "";
        int i = 0;

        foreach (Product item in _listProduct)
        {
            if (i != 3)
            {
                str += "<div style='margin-right: 5px; width: 31%; float: left;'><div style='width: 170px; min-width: 115px;' class='vmBrowseContainer v6BrowseContainer'>";
                str += "<div style='width: px; margin: 2px auto;' class='v6BrowseImageBox'>";
                str += String.Format("<a href='?act=product&id={0}'><img width='170' height='216' border='0' alt='G550' title='{1}' class='browseProductImage' src='images/products/{2}'></a>", item.ProductID, item.Name, item.Image);
                str += " <div class='v6BrowsePriceBox'><span class='productPrice'>$" + item.Price;
                str += "</span><br></div></div><br style='clear: both'><div class='vmBrowseProductTitle v6BrowseProductTitle'>";
                str += String.Format("<a href='?act=product&id={0}'>{1}</a>", item.ProductID, item.Name);
                str += "</div></div></div>";
                i++;
            }
            else
            {
                str += "<br class='clr'>";
                str += "<div style='margin-right: 5px; width: 31%; float: left;'><div style='width: 170px; min-width: 115px;' class='vmBrowseContainer v6BrowseContainer'>";
                str += "<div style='width: px; margin: 2px auto;' class='v6BrowseImageBox'>";
                str += String.Format("<a href='?act=product&id={0}'><img width='170' height='216' border='0' alt='G550' title='{1}' class='browseProductImage' src='images/products/{2}'></a>", item.ProductID, item.Name, item.Image);
                str += " <div class='v6BrowsePriceBox'><span class='productPrice'>" + item.Price;
                str += "</span><br></div></div><br style='clear: both'><div class='vmBrowseProductTitle v6BrowseProductTitle'>";
                str += String.Format("<a href='?act=product&id={0}'>{1}</a>", item.ProductID, item.Name);
                str += "</div></div></div>";
                i = 0;
            }
        }
        return str;
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string uri = Request.Url.AbsoluteUri;
        DropDownList ddl = (DropDownList)sender;
        switch (ddl.ID)
        {
            case "DropDownList1":

                if (Request.Params["sort"] != null)
                {
                    var a = uri.Split('&');
                    uri = uri.Replace((from b in a where b.Contains("sort=") select b).Single(), "sort=" + this.DropDownList1.SelectedValue.ToString());
                    Response.Redirect(uri);
                }
                break;
            case "DropDownList3":
                if (Request.Params["sortType"] != null)
                {
                    var a = uri.Split('&');
                    uri = uri.Replace((from b in a where b.Contains("sortType=") select b).Single(), "sortType=" + this.DropDownList3.SelectedValue.ToString());
                    Response.Redirect(uri);
                }
                break;
        }
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        //string uri = Request.Url.AbsoluteUri;

    }

    protected void DropDownList2_SelectedIndexChanged1(object sender, EventArgs e)
    {

    }
}
